package com.yqw.spring1219.dp.b_filter;

import java.util.ArrayList;
import java.util.List;

/**
 * 责任链整体也是看作是一个Filter
 * 
 * @author abc
 *
 */
public class FilterChain implements Filter {
	private List<Filter> filters = new ArrayList<>();
	int index = 0; //记录调用到第几个filter

	public FilterChain addFilter(Filter filter) {
		this.filters.add(filter);
		return this;
	}

	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		if (index == filters.size()) // 防止死循环
			return;
		Filter filter = filters.get(index);//调用第index个filter
		index++;
		filter.doFilter(request, response, chain); // 很像栈
	}
}
